package com.app.freshmenapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class NewsListActivity extends AppCompatActivity {
    ListView mLvNews;
    private ArrayList list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        initData();
        initView();
        initEvent();
    }

    private void initEvent() {

    }

    private void initView() {
        mLvNews = findViewById(R.id.lv_lzk);
    }

    private void initData() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                "http://182.61.26.165:8080/freshmenapp/news/list",
                "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                Type type = new TypeToken<MyBean>() {
                }.getType();
                MyBean myBean = gson.fromJson(jsonObject.toString(), type);
                List<MyBean.ListBean> list = myBean.getList();
                updateView(list);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });

        requestQueue.add(request);
    }

    private void updateView(List<MyBean.ListBean> list) {
        MyAdapter adapter = new MyAdapter(list);

        mLvNews.setAdapter(adapter);
    }
    class MyAdapter extends BaseAdapter {
        List list ;
        public MyAdapter(List<MyBean.ListBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyBean.ListBean bean = (MyBean.ListBean) list.get(position);

            View view = View.inflate(NewsListActivity.this,
                    R.layout.list_item,null);

            TextView mTvTitle = view.findViewById(R.id.Tv_title);
            TextView mTvDate = view.findViewById(R.id.Tv_dete);

            mTvTitle.setText(bean.getTitle());
            mTvDate.setText(bean.getDate());
            return view;
        }
    }
}
